package com.ubaer.zsycz.ui.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.lsw.Base.AdapterRefresh;
import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.AppManager;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;
import com.ubaer.zsycz.R;
import com.ubaer.zsycz.ui.adapter.FragmentViewPagerAdapter;
import com.ubaer.zsycz.ui.base.activity.BaseActivity;
import com.ubaer.zsycz.ui.dialog.ConfirmDialog;
import com.ubaer.zsycz.ui.dialog.RechargeProcessingDialog;
import com.ubaer.zsycz.ui.dialog.SuccessDialog;
import com.ubaer.zsycz.ui.fragment.AlreadyProcessedFragment;
import com.ubaer.zsycz.ui.fragment.RechargeFailureFragment;
import com.ubaer.zsycz.ui.fragment.UntreatedFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/9/25 0025.
 */

public class MainActivity extends BaseActivity implements AdapterRefresh {
    @BindView(R.id.mTabLayout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;
    UntreatedFragment untreatedFragment;
    AlreadyProcessedFragment alreadyProcessedFragment;
    RechargeFailureFragment rechargeFailureFragment;
    @BindView(R.id.iv_user)
    ImageView iv_user;
    @BindView(R.id.iv_seach)
    ImageView iv_seach;
    @BindView(R.id.rl_chouti)
    RelativeLayout rl_chouti;
    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.tv_seach)
    TextView tv_seach;
    //拨打客服
    @BindView(R.id.ll_call_phone)
    LinearLayout ll_call_phone;
    //退出登录
    @BindView(R.id.tv_loginout)
    TextView tv_loginout;
    ConfirmDialog confirmDialog;
    //账号
    @BindView(R.id.tv_zhnaghao)
    TextView tv_zhnaghao;

    private FragmentViewPagerAdapter viewPagerAdapter;
    private List<Fragment> fragments;

    private List<String> mTitleList = new ArrayList<>();//页卡标题集合

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initEventAndData() {
        confirmDialog = new ConfirmDialog(mContext);
        untreatedFragment = new UntreatedFragment();
        untreatedFragment.setAdapterRefresh(this);
        alreadyProcessedFragment = new AlreadyProcessedFragment();
        rechargeFailureFragment = new RechargeFailureFragment();

        mTitleList.add("未处理");
        mTitleList.add("充值成功");
        mTitleList.add("充值失败");
        fragments = new ArrayList<>();
        fragments.add(untreatedFragment);
        fragments.add(alreadyProcessedFragment);
        fragments.add(rechargeFailureFragment);
        viewPagerAdapter = new FragmentViewPagerAdapter(getSupportFragmentManager(), fragments, mTitleList);
        mViewPager.setAdapter(viewPagerAdapter);
        mTabLayout.setViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        //设置fragment不被回收
        mViewPager.setOffscreenPageLimit(3);

        iv_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(rl_chouti);
            }
        });
        iv_seach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SearchActivity.class);
                startActivity(intent);
            }
        });
        tv_seach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SearchActivity.class);
                startActivity(intent);
            }
        });
        ll_call_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtil.isNullOrEmpty(ShareUserInfoUtil.getInstance(mContext).getString(ShareUserInfoUtil.CUSTOMER_PHONE, "")))
                    call(ShareUserInfoUtil.getInstance(mContext).getString(ShareUserInfoUtil.CUSTOMER_PHONE, ""));
                else {
                    ToastUtil.showToast(mContext, "暂无客服电话！");
                }
            }
        });
        rl_chouti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tv_loginout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmDialog.showLoginout(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        confirmDialog.dismiss();
                        OpenLogin();
                    }
                });
            }
        });
        tv_zhnaghao.setText("账号" + ShareUserInfoUtil.getInstance(mContext).getString(ShareUserInfoUtil.USERNAME, ""));
    }

    @Override
    public void onRefreshAdapter(int position, int taskid) {
        if (taskid == 1) {
            mTabLayout.showDot(0);
        } else if (taskid == 0) {
            mTabLayout.hideMsg(0);
        } else if (taskid == 3) {
            //刷新充值成功
            if (alreadyProcessedFragment != null)
                alreadyProcessedFragment.onRefresh(null);
        } else if (taskid == 4) {
            //刷新充值失败
            if (rechargeFailureFragment != null)
                rechargeFailureFragment.onRefresh(null);
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // 点击一次返回按钮
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.showToast(mContext, "再按一次，退出应用");
                exitTime = System.currentTimeMillis();
            } else {
                AppManager.getAppManager().AppExit(mContext);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
